package cn.dinkevin.xui.widget.progress;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import cn.dinkevin.xui.R;

/**
 * 圆形进度条
 * Created by ChengPengFei on 2017/1/4 0004.
 */

public class CircleProgressView extends View {

    private int mProgress = 0;
    private final int mTxtStrokeWidth = 2;

    // 画圆所在的距形区域
    private final RectF mRectF;
    private final Paint mPaint;
    private final Context mContext;
    private String mTxtHint1;
    private String mTxtHint2;

    /**
     * 默认的进度颜色
     */
    private final int DEFAULT_PROGRESS_COLOR = Color.rgb(0xe9, 0xe9, 0xe9);
    private int mProgressColor;

    /**
     * 默认的进度条背景颜色
     */
    private final int DEFAULT_PROGRESS_BACKGROUND_COLOR = Color.TRANSPARENT;
    private int mProgressBackgroundColor;

    /**
     * 默认的进度条宽度
     */
    private final int DEFAULT_CIRCLE_LINE_STROKE_WIDTH = 8;
    private int mCircleLineStrokeWidth;

    /**
     * 默认的进度最大值
     */
    private final int DEFAULT_MAX_PROGRESS = 100;
    private int mMaxProgress;

    /**
     * 是否显示文字
     */
    private boolean mShowText = false;

    public CircleProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mContext = context;
        mRectF = new RectF();
        mPaint = new Paint();

        if(attrs == null) return;
        final TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.xui_circle_progress_view);
        mCircleLineStrokeWidth = attributes.getDimensionPixelSize(R.styleable.xui_circle_progress_view_progress_stroke_width,DEFAULT_CIRCLE_LINE_STROKE_WIDTH);
        mShowText = attributes.getBoolean(R.styleable.xui_circle_progress_view_show_text,false);
        mMaxProgress = attributes.getInt(R.styleable.xui_circle_progress_view_progress_max,DEFAULT_MAX_PROGRESS);
        mProgressBackgroundColor = attributes.getColor(R.styleable.xui_circle_progress_view_progress_background_color,DEFAULT_PROGRESS_BACKGROUND_COLOR);
        mProgressColor = attributes.getColor(R.styleable.xui_circle_progress_view_progress_color,DEFAULT_PROGRESS_COLOR);
        mProgress = attributes.getInt(R.styleable.xui_circle_progress_view_current_progress,0);
    }

    /**
     * 设置是否显示文字进度
     * @param showText
     */
    public void setShowText(boolean showText){
        mShowText = showText;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = this.getWidth();
        int height = this.getHeight();

        if (width != height) {
            int min = Math.min(width, height);
            width = min;
            height = min;
        }

        // 设置画笔相关属性
        mPaint.setAntiAlias(true);
        mPaint.setColor(mProgressBackgroundColor);
        canvas.drawColor(Color.TRANSPARENT);
        mPaint.setStrokeWidth(mCircleLineStrokeWidth);
        mPaint.setStyle(Paint.Style.STROKE);

        // 位置
        mRectF.left = mCircleLineStrokeWidth / 2; // 左上角x
        mRectF.top = mCircleLineStrokeWidth / 2; // 左上角y
        mRectF.right = width - mCircleLineStrokeWidth / 2; // 左下角x
        mRectF.bottom = height - mCircleLineStrokeWidth / 2; // 右下角y

        // 绘制圆圈，进度条背景
        canvas.drawArc(mRectF, -90, 360, false, mPaint);

        // 绘制进度
        mPaint.setColor(mProgressColor);
        canvas.drawArc(mRectF, -90, ((float) mProgress / mMaxProgress) * 360, false, mPaint);

        // 绘制进度文案显示
        if(mShowText)
        {
            mPaint.setStrokeWidth(mTxtStrokeWidth);
            String text = mProgress + "%";
            int textHeight = height / 4;
            mPaint.setTextSize(textHeight);
            int textWidth = (int) mPaint.measureText(text, 0, text.length());
            mPaint.setStyle(Paint.Style.FILL);
            canvas.drawText(text, width / 2 - textWidth / 2, height / 2 + textHeight / 2, mPaint);

            if (!TextUtils.isEmpty(mTxtHint1)) {
                mPaint.setStrokeWidth(mTxtStrokeWidth);
                text = mTxtHint1;
                textHeight = height / 8;
                mPaint.setTextSize(textHeight);
                mPaint.setColor(Color.rgb(0x99, 0x99, 0x99));
                textWidth = (int) mPaint.measureText(text, 0, text.length());
                mPaint.setStyle(Paint.Style.FILL);
                canvas.drawText(text, width / 2 - textWidth / 2, height / 4 + textHeight / 2, mPaint);
            }

            if (!TextUtils.isEmpty(mTxtHint2)) {
                mPaint.setStrokeWidth(mTxtStrokeWidth);
                text = mTxtHint2;
                textHeight = height / 8;
                mPaint.setTextSize(textHeight);
                textWidth = (int) mPaint.measureText(text, 0, text.length());
                mPaint.setStyle(Paint.Style.FILL);
                canvas.drawText(text, width / 2 - textWidth / 2, 3 * height / 4 + textHeight / 2, mPaint);
            }
        }
    }

    public int getMaxProgress() {
        return mMaxProgress;
    }

    public void setMaxProgress(int maxProgress) {
        this.mMaxProgress = maxProgress;
    }

    public void setProgress(int progress) {
        this.mProgress = progress;
        this.invalidate();
    }

    public int getProgress(){
        return mProgress;
    }

    public void setProgressNotInUiThread(int progress) {
        this.mProgress = progress;
        this.postInvalidate();
    }

    public String getmTxtHint1() {
        return mTxtHint1;
    }

    public void setmTxtHint1(String mTxtHint1) {
        this.mTxtHint1 = mTxtHint1;
    }

    public String getmTxtHint2() {
        return mTxtHint2;
    }

    public void setmTxtHint2(String mTxtHint2) {
        this.mTxtHint2 = mTxtHint2;
    }
}
